<?php

namespace App\Miya\Models;

use App\Miya\Traits\Scope\OrderByIdTrait;
use Illuminate\Database\Eloquent\Model;

class Keyword extends Model
{
    use OrderByIdTrait;

    protected $guarded = [];
    protected $casts = [
        'prefix' => 'json',
        'suffix' => 'json',
    ];

    public function getPrefixAttribute($value)
    {
        return array_values(json_decode($value, true) ?: [""]);
    }

    public function setPrefixAttribute($value)
    {
        $this->attributes['prefix'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function getSuffixAttribute($value)
    {
        return array_values(json_decode($value, true) ?: []);
    }

    public function setSuffixAttribute($value)
    {
        $this->attributes['suffix'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function getIgnoreAreasAttribute($value)
    {
        return array_values(json_decode($value, true) ?: []);
    }

    public function setIgnoreAreasAttribute($value)
    {
        $this->attributes['ignore_areas'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function area()
    {
        return $this->belongsTo(Area::class);
    }

    public function derivatives()
    {
        return $this->hasMany(DerivativeKeyword::class);
    }

    public function infos()
    {
        return $this->hasMany(Info::class);
    }

    public function modules()
    {
        return $this->hasMany(Module::class);
    }

    public function classifies()
    {
        return $this->hasMany(Classify::class);
    }
}
